package com.ibm.spatiotemp.server.framework.processor.fake;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import org.apache.log4j.Logger;

import com.ibm.spatiotemp.server.datasources.DataSource;
import com.ibm.spatiotemp.server.framework.processor.SpatialQueryProcessor;
import com.ibm.spatiotemp.server.framework.request.Request;
import com.ibm.spatiotemp.tools.DateParser;

public class FakeSpatialQueryProcessor extends SpatialQueryProcessor{
	public static Logger logger1 = Logger.getLogger(FakeSpatialQueryProcessor.class);
	protected BufferedReader freader;
	String filepath1 = "C:/eclipse/workplace/spatiotemporalservice/data/Cab1_Trajectory.csv";
	String filepath2 = "C:/eclipse/workplace/spatiotemporalservice/data/Cab2_Trajectory.csv";

	public FakeSpatialQueryProcessor(DataSource src) {
		super(src);
	}

	public static final String PositionFormat = "[%s,%s]";
	public String[] getCoordinates(String csvfilepath, long begtimestamp, long delta_mils) throws IOException{
		logger1.info(System.getProperty("user.dir"));
		freader = new BufferedReader(new FileReader(csvfilepath));
		String[] result = new String[2];
		
		StringBuffer pos_strbuf = new StringBuffer();
		StringBuffer ts_strbuf = new StringBuffer();
		String curline = null;
		long ts = begtimestamp;
		curline=freader.readLine();
		curline=freader.readLine();//skip head
		String[] splited;
		pos_strbuf.append("[");
		ts_strbuf.append("[");

		if(curline!=null){
			splited = curline.split(",");
			pos_strbuf.append(String.format(PositionFormat, splited[0],splited[1]));
			ts_strbuf.append("\""+ DateParser.FormatDate(ts) +"\"");
			ts += delta_mils;
		}
		
		while((curline=freader.readLine())!=null){
			splited = curline.split(",");
			pos_strbuf.append(",");
			pos_strbuf.append(String.format(PositionFormat, splited[0],splited[1]));
			ts_strbuf.append(",");
			ts_strbuf.append("\""+ DateParser.FormatDate(ts) +"\"");
			ts += delta_mils;
		}
		pos_strbuf.append("]");
		ts_strbuf.append("]");
		
		result[0] = pos_strbuf.toString();
		result[1] = ts_strbuf.toString();
		freader.close();
		return result;
	}
	
	
	public static Logger logger = Logger.getLogger(FakeSpatialQueryProcessor.class);
	/*
	 * Method: GET /tenant1?queryType=position&time=2014-01-01T00:00:20&&bbox=[-122.40327,37.52015,-122.3101,37.90572]
		Returns:
          [{
            "id": 锟斤拷uuid1",
            "key": "myMovingObject",
            锟斤拷trajectory": {
                                 "type": 锟斤拷Point",
                                 "coordinates": [25,36],
                                 "times": ["2014-01-01T00:00:20"]锟�6锟�5
                               }
              }, 
             {      "id": "uuid2",
              "key": "anotherMovingObject",
              锟斤拷trajectory": {
                                   "type": 锟斤拷Point",
                                   "coordinates": [400,2],
                                   "times": ["2014-01-01T00:00:20"]锟�6锟�5
                                 }
             }]
*/
	@Override
	public String handlePositionQuery(Request request) throws Exception {
		logger1.info(request.toJsonString()
				);
		String retstring =
				"["
				+ "{\"id\": \"uuid1\","
				+ "\"key\": \"myMovingObject\","
				+ "trajectory:{"
				+ "\"type\":\"Point\","
				+ "coordinates\": [37.75132,-122.39365], \"times\": [\"2014-01-01T00:00:20\"]"
				+ "}"
				+ "}"
				+ ","
				+ "{\"id\": \"uuid2\","
				+ "\"key\": \"myMovingObject\","
				+ "trajectory:{"
				+ "\"type\":\"Point\","
				+ "coordinates\": [37.75132,-122.39365], \"times\": [\"2014-01-01T00:00:20\"]"
				+ "}"
				+ "}"
				+ "]";
		return retstring;
	}

	/*type 1
	 * GET /tenant1?queryType=trajectory&id=uuid1&start=2014-01-01T00:00:00&end=2014-02-01T00:00:00*/
	/*
	 * Returns:
           [{
            "id": 锟斤拷uuid1",
            "key": "myMovingObject",
            锟斤拷trajectory锟斤拷: {
                                "type": "LineString",
                                "coordinates": [[-50,53], [25,36], [75,50], [150, 32]],          
                                "times": ["2014-01-01T00:00:00", "2014-01-01T00:00:20",锟�6锟�5
                                              "2014-01-01T00:00:30", "2014-01-01T00:00:55"]锟�6锟�5
                               }
            }]
            */
	/*type 2
	 * Method: GET /tenant1?queryType=trajectory&start=2014-01-01T00:00:00&end=2014-02-01T00:00:00&bbox=[-122.40327,37.52015,-122.3101,37.90572]&bboxRel=intersect*/
	/*
	 *Returns:  [
            {
            "id": 锟斤拷uuid1",
            "key": "myMovingObject",
            锟斤拷trajectory锟斤拷: {
                                "type": "LineString",
                                "coordinates": [[-50,53], [25,36], [75,50], [150, 32]],          
                                "times": ["2014-01-01T00:00:00", "2014-01-01T00:00:20",锟�6锟�5
                                              "2014-01-01T00:00:30", "2014-01-01T00:00:55"]锟�6锟�5
                               }
          },
          {
            "id": 锟斤拷uuid2",
           锟斤拷锟斤拷.
          },
          锟斤拷锟斤拷
       ]
	 * */
	/*
	 * 
37.75132,-122.39365,0,1211021854
37.75131,-122.39365,0,1211021907
37.75132,-122.39365,0,1211021972
37.75132,-122.39365,0,1211022028
	 * */
	@Override
	public String handleTrajectoryQuery(Request request) throws Exception {
		logger1.info(request.toJsonString());
		String retstring = "";
		if(request.getId()!= null)/*type1 query*/
		{
			String[] result = null;
			try {
				result = getCoordinates(filepath1,System.currentTimeMillis(),10000);
			} catch (IOException e) {
				e.printStackTrace();
			}
			retstring = "["
					+ "{"
					+ "\"id\": \"uuid1\","
					+ "\"key\": \"myMovingObject\","
					+ "\"trajectory\": {"
					+ "\"type\": \"Linestring\","
//					+ "\"coordinates\": [[37.75132,-122.39365], [37.75132,-122.39365], [37.75132,-122.39365], [37.75132,-122.39365]],"
//					+ "\"times\": [\"2014-01-01T00:00:00\", \"2014-01-01T00:00:20\",\"2014-01-01T00:00:30\", \"2014-01-01T00:00:55\"]"
					+ "\"coordinates\": %s,"
					+ "\"times\": %s"
					+ "}"
					+ "}"
					+ "]";
			retstring = String.format(retstring, result[0],result[1]);
		}
		else{/*type 2 query*/
			String[] result1 = null;
			String[] result2 = null;
			try {
				result1 = getCoordinates(filepath1,System.currentTimeMillis(),10000);
				result2 = getCoordinates(filepath2,System.currentTimeMillis(),10000);
			} catch (IOException e) {
				e.printStackTrace();
			}

			retstring = "["
					+ "{"
					+ "\"id\": \"uuid1\","
					+ "\"key\": \"myMovingObject\","
					+ "\"trajectory\": {"
					+ "\"type\": \"Linestring\","
//					+ "\"coordinates\": [[-50,53], [25,36], [75,50], [150, 32]],"
//					+ "\"times\": [\"2014-01-01T00:00:00\", \"2014-01-01T00:00:20\",\"2014-01-01T00:00:30\", \"2014-01-01T00:00:55\"]"
					+ "\"coordinates\": %s,"
					+ "\"times\": %s"
					+ "}"
					+ "}"
					+","
					+ "{"
					+ "\"id\": \"uuid2\","
					+ "\"key\": \"myMovingObject\","
					+ "\"trajectory\": {"
					+ "\"type\": \"Linestring\","
//					+ "\"coordinates\": [[-50,53], [25,36], [75,50], [150, 32]],"
//					+ "\"times\": [\"2014-01-01T00:00:00\", \"2014-01-01T00:00:20\",\"2014-01-01T00:00:30\", \"2014-01-01T00:00:55\"]"
					+ "\"coordinates\": %s,"
					+ "\"times\": %s"
					+ "}"
					+ "}"
			+ "]";
			retstring = String.format(retstring, result1[0],result1[1],result2[0],result2[1]);

		}
		return retstring;
	}

	/*
	 * 
	 * Method: GET /tenant1?queryType=firsttime&id=uuid1&position=[25,36]&distance=5&start=2014-01-01T00:00:00&end=2014-02-01T00:00:00
		Returns: 
          [{
            "id": 锟斤拷uuid1",
            "key": "myMovingObject",
            锟斤拷trajectory": { 
                                "type": 锟斤拷Point",
                                "coordinates": [25,36],
                                "times": ["2014-01-01T00:00:20"]
                               }
           }]
	 * */
	@Override
	public String handleTimeQuery(Request request) throws Exception {
		logger1.info(request.toJsonString());
		String retstring = "[{"
				+ "\"id\": ]\"uuid1\","
				+ "\"key\": \"myMovingObject\","
				+ "\"trajectory\":{"
				+ "\"type\":\"Point\""
				+ "\"coordinates\": [37.75132,-122.39365], \"times\": [\"2014-01-01T00:00:20\"]"
				+ "}}]";
		return retstring;
	}
	
}
